package us.versus.them.libounce;

import flash.display.MovieClip;
import flash.display.Graphics;

class App {
	var balls:haxe.FastList< Ball >;
	var lines:haxe.FastList< Line >;

	var root : MovieClip;

	public function new( ?root:MovieClip ) {

		this.root = if ( null == root ) { flash.Lib.current; } else { root; }
		this.root = flash.Lib.current;

		this.reset();
		this.root.addEventListener( flash.events.Event.ENTER_FRAME, update );
		this.root.stage.addEventListener( flash.events.MouseEvent.MOUSE_DOWN, down );
	}

	private function down( _ ) {
		reset();
	}

	private function reset() {
		this.init( this.root.stage.stageWidth, this.root.stage.stageHeight );
	}

	private function init( w:Float, h:Float ) {
		balls = new haxe.FastList< Ball >();
		lines = new haxe.FastList< Line >();

		for ( i in 0 ... 100 ) {
			balls.add( 
				new Ball( 
					  w * Math.random()
					, h * Math.random() 
				) 
			);
		}

		lines.add( new Line( 100,100, 260,260 ) );
		lines.add( new Line( 200,240, 400, 20 ) );
		lines.add( new Line( 20,220, 200,220 ) );

	}

	public function update( _ ) {
		this.draw( root.graphics, root.stage.stageWidth, root.stage.stageHeight );
	}

	public function draw( graphics:Graphics, w:Float, h:Float ) {
		graphics.clear();
		for ( ball in balls ) {
			ball.move( lines, w, h );
			ball.draw( graphics, w, h );
		}

		for ( line in lines ) {
			line.draw( graphics );
		}
	}


}
